(function(angular) {
	'use strict';
	angular.module('fs.waf').controller('cda.popup.profAcctEditController', profAcctEditController);
	profAcctEditController.$inject = [ '$scope', '$q', '$uibModalInstance', 'Constants', 'dialogsModule', 'gridService', 'modalService', 'layoutConfig' , 'authService', 'ajaxServiceFactory', 'validatorConfig'];
	function profAcctEditController($scope, $q, $uibModalInstance, Constants, dialogsModule, gridService, modalService, layoutConfig, authService, ajaxServiceFactory, validatorConfig) {
		var ctrl = this, log = $scope.loggerFactory.getLogger("profAcctEditController"), ajaxService = ajaxServiceFactory.buildAjaxService({
			getProfAcctOptions : {
				url : 'do/cda/dataList/profAcctOptions',
				method : 'post'
			},
		});
		ctrl.dialogsModule = dialogsModule;
		ctrl.layoutConfig = layoutConfig;
		ctrl.validatorConfig = validatorConfig;
		ctrl.filterEventCodes = ctrl.dialogsModule.params.filterEventCodes || [];
		ctrl.prodType = ctrl.dialogsModule.params.prodType;
		ctrl.updateProcessFlag = ctrl.dialogsModule.params.updateProcessFlag;
		ctrl.isEdit = ctrl.dialogsModule.params.isEdit;
		ctrl.profAcct = {};
		ctrl.formName = "profAcctForm";
		
		ctrl.init = function() {
			ctrl.initGeneral().then(function() {
				ctrl.profAcct = ctrl.dialogsModule.params.profAcct || {};
			});
		};
		
		ctrl.initGeneral = function() {
			var defer = $q.defer();
			
			var params = {};
			params.prodType = ctrl.prodType;
			getProfAcctOptions(params).then(function(response) {
				ctrl.eventCodeOptions = response.eventCodeOptions;
				ctrl.yesNoOptions = response.yesNoOptions;
				ctrl.categoryOptions = response.categoryOptions;
				defer.resolve();
			}, angular.noop);
			
			ctrl.acctRuleInnerGridOptions = gridService.createInnerGridOptions($scope, ctrl.layoutConfig.formConfigs.acctRule.tableConfigs.table, ctrlAcctRuleRowSelectionChanged);
			ctrl.acctRuleInnerGridOptions.data = 'vm.profAcct.acctRules';
			ctrl.profSubAcctInnerGridOptions = gridService.createInnerGridOptions($scope, ctrl.layoutConfig.formConfigs.profSubAcct.tableConfigs.table, ctrlProfSubAcctRowSelectionChanged);
			ctrl.profSubAcctInnerGridOptions.data = 'vm.profAcct.profSubAccts';
			
			return defer.promise;
		};
		function getProfAcctOptions(params) {
			var defer = $q.defer();
			ajaxService.getProfAcctOptions(params).then(function(response) {
				var profAcctOptions = {};
				if (response.data) {
					angular.forEach(response.data, function(value, key) {
						var tempValue = {};
//						tempValue.data = value;
						tempValue = value;
						profAcctOptions[key] = tempValue;
					});
				}
				defer.resolve(profAcctOptions);
			});
			return defer.promise;
		}
		
		ctrl.eventCodeChanged = function(value, form , firstLoad) {
			if (ctrl.profAcct.eventCode) {
				for (var i = 0; i < ctrl.eventCodeOptions.length; i++) {
					var _event = ctrl.eventCodeOptions[i];
					if (_event.value == ctrl.profAcct.eventCode) {
						if (ctrl.isEdit != 'Y') {// add page
							//
							angular.forEach(ctrl.filterEventCodes, function(filterEventCode, index) {
								if (ctrl.profAcct.eventCode == filterEventCode) {
									// changed eventCode has used
									ctrl.profAcct.eventCode = '';
									modalService.openErrorDialogs({
										messages : [ 'js.info.profacct.duplicate-eventcode' ]
									});
									return;
								}
							});
							// if eventCode change, need empty value
							ctrl.profAcct.acctRules = [];
							ctrl.profAcct.profSubAccts = [];
							ctrl.profAcct.drAcctCode = '';
							ctrl.profAcct.drAcctDesc = '';
							ctrl.profAcct.crAcctCode = '';
							ctrl.profAcct.crAcctDesc = '';
							ctrl.profAcct.acctRuleFlag = 'N';
						}
						//
						resetAcctByEventExtProperty(ctrl, _event);
						break;
					}
				}
			}
		};
		
		function resetAcctByEventExtProperty (ctrl , _event) {
			ctrl.profAcct.eventExtProperty = _event.extProperty;
			// has serviceSubEvents, mean has prof acct
			if (ctrl.profAcct.eventExtProperty.serviceSubEvents && ctrl.profAcct.eventExtProperty.serviceSubEvents.length > 0) {
				ctrl.profAcct.subeventFlag = 'Y';
			} else {
				ctrl.profAcct.subeventFlag = 'N';
			}
			
			ctrl.profAcct.crAcctType = ctrl.profAcct.eventExtProperty.glCrAcctType;
			ctrl.profAcct.drAcctType = ctrl.profAcct.eventExtProperty.glDrAcctType;
			
			// for init change row.entity readonly or not
			if (ctrl.profAcct.acctRules) {
				angular.forEach(ctrl.profAcct.acctRules, function(acctRule, key) {
					acctRule.glDrAcctFlag = ctrl.profAcct.eventExtProperty.glDrAcctFlag;
					acctRule.glCrAcctFlag = ctrl.profAcct.eventExtProperty.glCrAcctFlag;
				});
			} 
		}
		
		
		function ctrlAcctRuleRowSelectionChanged(eventRow, selectedRows) {
			ctrl.selectedAcctRules = selectedRows;
		}
		
		ctrl.addAcctRule = function(currRow) {
			return $q(function(resolved, rejected){
				currRow.glDrAcctFlag = ctrl.profAcct.eventExtProperty.glDrAcctFlag;
				currRow.glCrAcctFlag = ctrl.profAcct.eventExtProperty.glCrAcctFlag;
				resolved();
			});
		};
		

		function ctrlProfSubAcctRowSelectionChanged(eventRow, selectedRows) {
			ctrl.selectedProfSubAccts = selectedRows;
		}
		
		ctrl.addProfSubAcct = function(currRow) {
			return $q(function(resolved, rejected){
				// params
				var params = {};
				params.filterEventCodes = [];
				angular.forEach(ctrl.profAcct.profSubAccts, function(profSubAcct, index) {
					params.filterEventCodes.push(profSubAcct.eventCode);
				});
				params.serviceSubEvents = ctrl.profAcct.eventExtProperty.serviceSubEvents;// for subAcct eventCode
				modalService.openCustomDialogs({
				   'title': 'Prof Sub Acct Edit',
				   'dialogsModule' : 'cda.profsubacct.popupedit',
				   'params': params,
				   'btnCallback' : {
					   'ok' : function(profSubAcct) {							
							angular.merge(currRow, profSubAcct);
							resolved();
						}
					}		
				});
			});
		};
		
		ctrl.editProfSubAcct = function(currRow) {
			var params = {};
			// for show selected service on popup page
			var _selectedProfSubAcct = currRow.entity;
			params.profSubAcct = angular.copy(_selectedProfSubAcct);
			params.filterEventCodes = [];
			angular.forEach(ctrl.profAcct.profSubAccts, function(profSubAcct, index) {
				params.filterEventCodes.push(profSubAcct.eventCode);
			});
			params.serviceSubEvents = ctrl.profAcct.eventExtProperty.serviceSubEvents;// for subAcct eventCode
			params.isEdit = 'Y';
			modalService.openCustomDialogs({
				   'title': 'Prof Sub Acct Edit',
				   'dialogsModule' : 'cda.profsubacct.popupedit',
				   'params': params,
				   'btnCallback' : {
						'ok' : function(profSubAcct) {							
							if (!ctrl.profAcct.profSubAccts) {								
								ctrl.profAcct.profSubAccts = [];
							}
							if (!(profSubAcct.dataStatus == 'C' || profSubAcct.dataStatus == 'U')) {
								if (ctrl.updateProcessFlag) {
									profSubAcct.dataStatus = 'U';
								} else {
									profSubAcct.dataStatus = 'C';
								}
							}
							angular.extend(_selectedProfSubAcct, profSubAcct);
						}
					}		
				   
			});			
		};
		
		
		ctrl.ok = function() {
			if ($scope.profAcctForm) {
				$scope.profAcctForm.$setSubmitted();
				if(!$scope.profAcctForm.$valid) {
					dialogsModule.showErrorMessages($scope.profAcctForm);
					return;
				}
			}
			if ((!$scope.profAcctForm || $scope.profAcctForm.$valid) && (!dialogsModule.btnCallback || !dialogsModule.btnCallback['ok'] || dialogsModule.btnCallback['ok'](ctrl.profAcct) !== false)) {
				$uibModalInstance.close();
			}
		};
		
		ctrl.close = function() {
			if (!dialogsModule.btnCallback || !dialogsModule.btnCallback['close'] || dialogsModule.btnCallback['close']() !== false) {
				$uibModalInstance.dismiss();
			}
		};
		
	}
})(angular);
